﻿using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartParking.Entities;
using SmartParking.IService;
using SmartParking.Models;
using SmartParking.Service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using SmartParking.Base;


namespace SmartParking.ViewModels.Pages
{
    public class UserManagementViewModel : ViewModelBase<UserModel>
    {
        public ObservableCollection<UserModelEx> Users = [];

        IUserService _userService;
        IDialogService _dialogService;

        public DelegateCommand<UserModel> ResetPasswordCommand { get; set; }
        public DelegateCommand<UserModel> LockUserCommand { get; set; }
        public DelegateCommand<UserModel> EditUserRoleCommand { get; set; }

        public UserManagementViewModel(IRegionManager regionManager,
            IUserService userService,
            IDialogService dialogService) : base(regionManager)
        {
            PageTitle = "系统用户";

            _userService = userService;
            _dialogService = dialogService;

            ResetPasswordCommand = new DelegateCommand<UserModel>(DoResetPassword);
            LockUserCommand = new DelegateCommand<UserModel>(DoLockUser);
            EditUserRoleCommand = new DelegateCommand<UserModel>(DoEditUserRole);

            this.Refresh();

           
        }

        public override void Refresh()
        {
            Users.Clear();

            var datas = _userService.GetUsers(SearchKey);
            int index = 1;
            foreach (var user in datas)
            {
                Users.Add(new UserModelEx
                {
                    Index = index++,
                    UserId = user.UserId,
                    UserName = user.UserName,
                    RealName = user.RealName,
                    UserIcon = "pack://siteoforigin:,,,/Avatars/" + user.UserIcon,
                    Address = user.Address,
                    Age = user.Age,
                    Password = user.Password,
                    Gender = user.Gender,
                    Email = user.Email,
                    Status = user.Status,

                    LockButtonText = user.Status == 1 ? "锁定" : "启用"
                });
            }
        }

        public override void DoModify(UserModel user)
        {
            var parameter = new DialogParameters
            {
                { "User", user }
            };

            _dialogService.ShowDialog("ModifyUserView", parameter, r =>
            {
                if (r.Result == ButtonResult.OK)
                {
                    Refresh();
                }
            });
        }

        public override void DoDelete(UserModel user)
        {
            if (user.UserId > 0)
            {
                if (MessageBox.Show("确定要删除该用户吗？", "删除用户", MessageBoxButton.YesNo) == MessageBoxResult.No)
                    return;
                
                _userService.Delete<SysUser>(user.UserId);
                Users.Remove(user as UserModelEx);

                MessageBox.Show("删除成功!","提示");

                Refresh();
            }
        }

        public void DoResetPassword(UserModel user) 
        {
            if (user.UserId > 0)
            {
                if (MessageBox.Show("确定重置该用户密码吗？", "重置密码", MessageBoxButton.YesNo) == MessageBoxResult.No)
                    return;

                var userData = _userService.Find<SysUser>(user.UserId);
                userData.Password = "123456";

                _userService.Update(userData);

                MessageBox.Show("重置成功!", "提示");

            }
        }

        public void DoLockUser(UserModel user) 
        {
            if (user.UserId > 0)
            {
                if (MessageBox.Show("确定要锁定该用户吗？", "锁定用户", MessageBoxButton.YesNo) == MessageBoxResult.No)
                    return;

                var userData = _userService.Find<SysUser>(user.UserId);
                userData.Status = 0;

                _userService.Update(userData);

                MessageBox.Show("锁定成功!", "提示");

                Refresh();
            }
        }

        public void DoEditUserRole(UserModel user) 
        {
            
        }
    }
}
